package com.software.service;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    private Button btn_end;
    private Button btn_start;
    private Button btn_unbind;
    private Button btn_bind;
    private ServiceConnection conn;
    private Button btn_exit;
    private Button btn_stop;
    private Button btn_pause;
    private Button btn_play;
    private Button btn_download;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
        initEvents();
    }

    private void initEvents() {
        btn_start.setOnClickListener(v -> {
            Intent intent = new Intent(this, LifeService.class);
            startService(intent);
        });

        btn_end.setOnClickListener(v -> {
            Intent intent = new Intent(this, LifeService.class);
            stopService(intent);
        });

        btn_bind.setOnClickListener(v -> {
            Intent intent = new Intent(this, LifeService.class);
            conn = new ServiceConnection() {
                @Override
                public void onServiceConnected(ComponentName name, IBinder service) {
                    Log.i("lifeService", "onServiceConnected");

                }

                @Override
                public void onServiceDisconnected(ComponentName name) {
                    Log.i("lifeService", "onServiceDisconnected");

                }
            };
            bindService(intent, conn, Context.BIND_AUTO_CREATE);
        });

        btn_unbind.setOnClickListener(v -> {
            unbindService(conn);
        });

        btn_play.setOnClickListener(new PlayEvents());
        btn_pause.setOnClickListener(new PlayEvents());
        btn_stop.setOnClickListener(new PlayEvents());
        btn_exit.setOnClickListener(new PlayEvents());

        // intentService
        btn_download.setOnClickListener(v -> {
            Intent intent = new Intent(this, DownloadService.class);
            startService(intent);
        });
    }


    private void initViews() {
        btn_start = findViewById(R.id.btn_start);
        btn_end = findViewById(R.id.btn_end);
        btn_bind = findViewById(R.id.btn_bind);
        btn_unbind = findViewById(R.id.btn_unbind);

        btn_play = findViewById(R.id.btn_play);
        btn_pause = findViewById(R.id.btn_pause);
        btn_stop = findViewById(R.id.btn_stop);
        btn_exit = findViewById(R.id.btn_exit);
        btn_download = findViewById(R.id.btn_download);
    }

    class PlayEvents implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            int id = v.getId();
            Intent intent = new Intent(MainActivity.this, PlayMediaService.class);
            if (id == R.id.btn_play) {
                intent.putExtra("media", "play");
                startService(intent);
            } else if (id == R.id.btn_pause) {
                intent.putExtra("media", "pause");
                startService(intent);
            } else if (id == R.id.btn_stop) {
                intent.putExtra("media", "stop");
                startService(intent);
            } else if (id == R.id.btn_exit) {

            }
        }
    }
}